package cn.bdqn.t354.web.interceptor;

import cn.bdqn.t354.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class AuthorizationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户是否登录
        User user=(User) request.getSession().getAttribute("loginUser");
        if(user==null){
            //引导用户来到登录页面
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print("<script>alert('没有登录；请先去登录(Interceptor)');location.href='/login';</script>");
            return false;
        }
        return true;
    }
}
